#include <cfloat>
#include <cmath>
#include "KnnClassifier.h"

namespace fc 
{


void KnnClassifier::setTrainingData ( const vector<Example> & data, int attributes )
{
    trains = data;
    attrs = attributes;
}

Result KnnClassifier::classify(const vector< double >& example ) const
{
    Result result(0, 1);
    double measure = DBL_MAX;
    for(vector<Example>::const_iterator it = trains.begin(); it != trains.end(); ++it)
    {
        double m = findMeasure(*it, example, attrs);
        if(measure > m)
        {
            measure = m;
            result.resultClass = it->consequent;
        }
    }
    return result;
}

double KnnClassifier::findMeasure(const Example & example, const vector< double > & testExample, int attributes)
{
    double sum = 0;
    for(int i = 0; i < attributes; ++i)
        sum += pow(example.antacedent[i] - testExample[i], 2);
    return sqrt(sum);
}

}